<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>~/dev/komasshu-demo/offlinesample/frameworks/us-express/public/javascripts/script.js.html</title>
<meta name="Generator" content="Vim/7.3">
<meta name="plugin-version" content="vim7.3_v8">
<meta name="syntax" content="javascript">
<meta name="settings" content="number_lines,use_css,expand_tabs">
<style type="text/css">
<!--
pre { font-family: monospace; color: #000000; background-color: #ffffff; }
body { font-family: monospace; color: #000000; background-color: #ffffff; }
.lnr { color: #888888; background-color: #e6e6e6; }
.Boolean { color: #cd0000; }
.Statement { color: #b03060; font-weight: bold; }
.String { color: #4a708b; }
.Identifier { color: #458b74; }
.Comment { color: #0000ee; font-style: italic; }
-->
</style>
</head>
<body>
<pre>
<span class="lnr">  1 </span><span class="Comment">/* Author: Kensaku KOMATSU</span>
<span class="lnr">  2 </span>
<span class="lnr">  3 </span><span class="Comment">*/</span>
<span class="lnr">  4 </span>
<span class="lnr">  5 </span><span class="Comment">/**</span>
<span class="lnr">  6 </span><span class="Comment"> * Collections</span>
<span class="lnr">  7 </span><span class="Comment"> *</span>
<span class="lnr">  8 </span><span class="Comment"> */</span>
<span class="lnr">  9 </span>
<span class="lnr"> 10 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr"> 11 </span><span class="Comment">// _.each</span>
<span class="lnr"> 12 </span><span class="Comment">// _.each(list, iterator, [context]) Alias: forEach</span>
<span class="lnr"> 13 </span><span class="Comment">//</span>
<span class="lnr"> 14 </span><span class="Comment">// list内の要素(element)に対し、iteratorで定義された関数処理を</span>
<span class="lnr"> 15 </span><span class="Comment">// 繰り返し実行する。listがArrayの場合、iteratorに渡される</span>
<span class="lnr"> 16 </span><span class="Comment">// 引数は(element, index, list), objectの場合は、</span>
<span class="lnr"> 17 </span><span class="Comment">// (value, key, list)となる。</span>
<span class="lnr"> 18 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr"> 19 </span>
<span class="lnr"> 20 </span><span class="Comment">// listがArrayの場合</span>
<span class="lnr"> 21 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr"> 22 </span>  <span class="Identifier">var</span> sum = <span class="Identifier">[</span>0, 0<span class="Identifier">]</span>;
<span class="lnr"> 23 </span>  _.each(<span class="Identifier">[</span>2,4,6<span class="Identifier">]</span>, <span class="Identifier">function</span>(num, idx, list)<span class="Identifier">{</span>
<span class="lnr"> 24 </span>    sum<span class="Identifier">[</span>0<span class="Identifier">]</span> += num * <span class="Identifier">this</span>.a;
<span class="lnr"> 25 </span>    sum<span class="Identifier">[</span>1<span class="Identifier">]</span> += idx;
<span class="lnr"> 26 </span>  <span class="Identifier">}</span>, <span class="Identifier">{</span>a:3<span class="Identifier">}</span>);
<span class="lnr"> 27 </span>  console.log(sum);
<span class="lnr"> 28 </span>  <span class="Comment">// #=&gt; [36,3]</span>
<span class="lnr"> 29 </span><span class="Identifier">}</span>)();
<span class="lnr"> 30 </span><span class="Comment">// listがObjectの場合</span>
<span class="lnr"> 31 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr"> 32 </span>  <span class="Identifier">var</span> sum = <span class="Identifier">[</span>0, <span class="String">&quot;&quot;</span><span class="Identifier">]</span>;
<span class="lnr"> 33 </span>  _.each(<span class="Identifier">{</span>a:2,b:4,c:6<span class="Identifier">}</span>, <span class="Identifier">function</span>(val, key, list)<span class="Identifier">{</span>
<span class="lnr"> 34 </span>    sum<span class="Identifier">[</span>0<span class="Identifier">]</span> += val * <span class="Identifier">this</span>.a;
<span class="lnr"> 35 </span>    sum<span class="Identifier">[</span>1<span class="Identifier">]</span> += key;
<span class="lnr"> 36 </span>  <span class="Identifier">}</span>, <span class="Identifier">{</span>a:3<span class="Identifier">}</span>);
<span class="lnr"> 37 </span>  console.log(sum);
<span class="lnr"> 38 </span>  <span class="Comment">// #=&gt; [36,&quot;abc&quot;]</span>
<span class="lnr"> 39 </span><span class="Identifier">}</span>)();
<span class="lnr"> 40 </span>
<span class="lnr"> 41 </span>
<span class="lnr"> 42 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr"> 43 </span><span class="Comment">// _.map</span>
<span class="lnr"> 44 </span><span class="Comment">// _.map(list, iterator, [context])</span>
<span class="lnr"> 45 </span><span class="Comment">//</span>
<span class="lnr"> 46 </span><span class="Comment">// list内の各要素にiteratorで定義された関数処理に従い、</span>
<span class="lnr"> 47 </span><span class="Comment">// 新たなArrayを生成する。listがArrayの場合、iteratorに</span>
<span class="lnr"> 48 </span><span class="Comment">// 渡される引数は、listの各要素値、Objectの場合は、</span>
<span class="lnr"> 49 </span><span class="Comment">// （value, key)となる。</span>
<span class="lnr"> 50 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr"> 51 </span>
<span class="lnr"> 52 </span><span class="Comment">// listがArrayの場合</span>
<span class="lnr"> 53 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr"> 54 </span>  <span class="Identifier">var</span> arr = _.map(<span class="Identifier">[</span>2,4,6<span class="Identifier">]</span>, <span class="Identifier">function</span>(num)<span class="Identifier">{</span>
<span class="lnr"> 55 </span>    <span class="Statement">return</span> num*<span class="Identifier">this</span>.a;
<span class="lnr"> 56 </span>  <span class="Identifier">}</span>, <span class="Identifier">{</span>a:3<span class="Identifier">}</span>);
<span class="lnr"> 57 </span>  console.log(arr);
<span class="lnr"> 58 </span>  <span class="Comment">// #=&gt; [6,12,18]</span>
<span class="lnr"> 59 </span><span class="Identifier">}</span>)();
<span class="lnr"> 60 </span><span class="Comment">// listがObjectの場合</span>
<span class="lnr"> 61 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr"> 62 </span>  <span class="Identifier">var</span> arr = _.map(<span class="Identifier">{</span>a:2,b:4,c:6<span class="Identifier">}</span>, <span class="Identifier">function</span>(val, key)<span class="Identifier">{</span>
<span class="lnr"> 63 </span>    <span class="Statement">return</span> val * <span class="Identifier">this</span>.a;
<span class="lnr"> 64 </span>  <span class="Identifier">}</span>, <span class="Identifier">{</span>a:3<span class="Identifier">}</span>);
<span class="lnr"> 65 </span>  console.log(arr);
<span class="lnr"> 66 </span>  <span class="Comment">// #=&gt; [6,12,18]</span>
<span class="lnr"> 67 </span><span class="Identifier">}</span>)();
<span class="lnr"> 68 </span>
<span class="lnr"> 69 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr"> 70 </span><span class="Comment">// _.reduce</span>
<span class="lnr"> 71 </span><span class="Comment">// _.reduce(list, iterator, memo, [context]) Aliases: inject, foldl</span>
<span class="lnr"> 72 </span><span class="Comment">//</span>
<span class="lnr"> 73 </span><span class="Comment">// listで与えられたArrayもしくはObjectから、iteratorで渡された関数処理に</span>
<span class="lnr"> 74 </span><span class="Comment">// 従い、一つの値を得る。memoは、生成する値の初期値。</span>
<span class="lnr"> 75 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr"> 76 </span>
<span class="lnr"> 77 </span><span class="Comment">// listがArrayの場合</span>
<span class="lnr"> 78 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr"> 79 </span>  <span class="Identifier">var</span> ret = _.reduce(<span class="Identifier">[</span>2,4,6<span class="Identifier">]</span>, <span class="Identifier">function</span>(memo, num)<span class="Identifier">{</span>
<span class="lnr"> 80 </span>    <span class="Statement">return</span> memo + num*<span class="Identifier">this</span>.a;
<span class="lnr"> 81 </span>  <span class="Identifier">}</span>, 1, <span class="Identifier">{</span>a:3<span class="Identifier">}</span>);
<span class="lnr"> 82 </span>  console.log(ret);
<span class="lnr"> 83 </span>  <span class="Comment">// #=&gt; 37</span>
<span class="lnr"> 84 </span><span class="Identifier">}</span>)();
<span class="lnr"> 85 </span><span class="Comment">// listがObjectの場合</span>
<span class="lnr"> 86 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr"> 87 </span>  <span class="Identifier">var</span> ret = _.reduce(<span class="Identifier">{</span>a:2,b:4,c:6<span class="Identifier">}</span>, <span class="Identifier">function</span>(memo, val, key)<span class="Identifier">{</span>
<span class="lnr"> 88 </span>    <span class="Statement">return</span> memo + val * <span class="Identifier">this</span>.a;
<span class="lnr"> 89 </span>  <span class="Identifier">}</span>, 1, <span class="Identifier">{</span>a:3<span class="Identifier">}</span>);
<span class="lnr"> 90 </span>  console.log(ret);
<span class="lnr"> 91 </span>  <span class="Comment">// #=&gt; 37</span>
<span class="lnr"> 92 </span><span class="Identifier">}</span>)();
<span class="lnr"> 93 </span>
<span class="lnr"> 94 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr"> 95 </span><span class="Comment">// _.reduceRight</span>
<span class="lnr"> 96 </span><span class="Comment">// _.reduceRight(list, iterator, memo, [context]) Alias: foldr</span>
<span class="lnr"> 97 </span><span class="Comment">//</span>
<span class="lnr"> 98 </span><span class="Comment">// reduceがlist内の要素を左から処理するのに対し、reduceRightでは</span>
<span class="lnr"> 99 </span><span class="Comment">// 右から処理する。低速動作となるため、あまり有用ではない。</span>
<span class="lnr">100 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">101 </span>
<span class="lnr">102 </span><span class="Comment">// listがArrayの場合</span>
<span class="lnr">103 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">104 </span>  <span class="Identifier">var</span> ret = _.reduceRight(<span class="Identifier">[</span>2,4,6<span class="Identifier">]</span>, <span class="Identifier">function</span>(memo, num)<span class="Identifier">{</span>
<span class="lnr">105 </span>    <span class="Statement">return</span> memo.concat(num * <span class="Identifier">this</span>.a);
<span class="lnr">106 </span>  <span class="Identifier">}</span>, <span class="Identifier">[]</span>, <span class="Identifier">{</span>a:3<span class="Identifier">}</span>);
<span class="lnr">107 </span>  console.log(ret);
<span class="lnr">108 </span>  <span class="Comment">// #=&gt; [18, 12, 6]</span>
<span class="lnr">109 </span><span class="Identifier">}</span>)();
<span class="lnr">110 </span><span class="Comment">// listがObjectの場合</span>
<span class="lnr">111 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">112 </span>  <span class="Identifier">var</span> ret = _.reduceRight(<span class="Identifier">{</span>a:2,b:4,c:6<span class="Identifier">}</span>, <span class="Identifier">function</span>(memo, val, key)<span class="Identifier">{</span>
<span class="lnr">113 </span>    <span class="Statement">return</span> memo.concat(val * <span class="Identifier">this</span>.a);
<span class="lnr">114 </span>  <span class="Identifier">}</span>, <span class="Identifier">[]</span>, <span class="Identifier">{</span>a:3<span class="Identifier">}</span>);
<span class="lnr">115 </span>  console.log(ret);
<span class="lnr">116 </span>  <span class="Comment">// #=&gt; [18, 12, 6]</span>
<span class="lnr">117 </span><span class="Identifier">}</span>)();
<span class="lnr">118 </span>
<span class="lnr">119 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">120 </span><span class="Comment">// _.detect</span>
<span class="lnr">121 </span><span class="Comment">// _.detect(list, iterator, [context])</span>
<span class="lnr">122 </span><span class="Comment">//</span>
<span class="lnr">123 </span><span class="Comment">// list内の各要素に対して、iteratorで定義された処理を実行し、</span>
<span class="lnr">124 </span><span class="Comment">// 最初にtrueとなる要素を返す。trueが検出された時点で、iterator</span>
<span class="lnr">125 </span><span class="Comment">// の処理は終了となり、listの全要素を処理することはない。</span>
<span class="lnr">126 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">127 </span>
<span class="lnr">128 </span><span class="Comment">// listがArrayの場合</span>
<span class="lnr">129 </span>console.log(<span class="String">&quot;_detect&quot;</span>);
<span class="lnr">130 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">131 </span>  <span class="Identifier">var</span> ret = _.detect(<span class="Identifier">[</span>2,4,6<span class="Identifier">]</span>, <span class="Identifier">function</span>(num)<span class="Identifier">{</span>
<span class="lnr">132 </span>    <span class="Statement">return</span> num*<span class="Identifier">this</span>.a == 12;
<span class="lnr">133 </span>  <span class="Identifier">}</span>, <span class="Identifier">{</span>a:3<span class="Identifier">}</span>);
<span class="lnr">134 </span>  console.log(ret);
<span class="lnr">135 </span>  <span class="Comment">// #=&gt; 4</span>
<span class="lnr">136 </span><span class="Identifier">}</span>)();
<span class="lnr">137 </span><span class="Comment">// listがObjectの場合</span>
<span class="lnr">138 </span>console.log(<span class="String">&quot;_detect&quot;</span>);
<span class="lnr">139 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">140 </span>  <span class="Identifier">var</span> ret = _.detect(<span class="Identifier">{</span>a:2,b:4,c:6<span class="Identifier">}</span>, <span class="Identifier">function</span>(val, key)<span class="Identifier">{</span>
<span class="lnr">141 </span>    <span class="Statement">return</span> val*<span class="Identifier">this</span>.a == 12;
<span class="lnr">142 </span>  <span class="Identifier">}</span>, <span class="Identifier">{</span>a:3<span class="Identifier">}</span>);
<span class="lnr">143 </span>  console.log(ret);
<span class="lnr">144 </span>  <span class="Comment">// #=&gt; 4</span>
<span class="lnr">145 </span><span class="Identifier">}</span>)();
<span class="lnr">146 </span>
<span class="lnr">147 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">148 </span><span class="Comment">// _.select</span>
<span class="lnr">149 </span><span class="Comment">// _.select(list, iterator, [context]) Alias: filter</span>
<span class="lnr">150 </span><span class="Comment">//</span>
<span class="lnr">151 </span><span class="Comment">// list内の要素に対し、iteratorで定義された処理に対し、</span>
<span class="lnr">152 </span><span class="Comment">// trueを返す要素のみの配列を返す。</span>
<span class="lnr">153 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">154 </span>
<span class="lnr">155 </span><span class="Comment">// listがArrayの場合</span>
<span class="lnr">156 </span>console.log(<span class="String">&quot;_select&quot;</span>);
<span class="lnr">157 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">158 </span>  <span class="Identifier">var</span> ret = _.select(<span class="Identifier">[</span>2,3,4,5,6<span class="Identifier">]</span>, <span class="Identifier">function</span>(num)<span class="Identifier">{</span>
<span class="lnr">159 </span>    <span class="Statement">return</span> num % <span class="Identifier">this</span>.a == 0;
<span class="lnr">160 </span>  <span class="Identifier">}</span>, <span class="Identifier">{</span>a:2<span class="Identifier">}</span>);
<span class="lnr">161 </span>  console.log(ret);
<span class="lnr">162 </span>  <span class="Comment">// #=&gt; [2, 4, 6]</span>
<span class="lnr">163 </span><span class="Identifier">}</span>)();
<span class="lnr">164 </span><span class="Comment">// listがObjectの場合</span>
<span class="lnr">165 </span>console.log(<span class="String">&quot;_select&quot;</span>);
<span class="lnr">166 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">167 </span>  <span class="Identifier">var</span> ret = _.select(<span class="Identifier">{</span>a:2,b:3,c:4<span class="Identifier">}</span>, <span class="Identifier">function</span>(val, key)<span class="Identifier">{</span>
<span class="lnr">168 </span>    <span class="Statement">return</span> val % <span class="Identifier">this</span>.a == 0;
<span class="lnr">169 </span>  <span class="Identifier">}</span>, <span class="Identifier">{</span>a:2<span class="Identifier">}</span>);
<span class="lnr">170 </span>  console.log(ret);
<span class="lnr">171 </span>  <span class="Comment">// #=&gt; [2, 4]</span>
<span class="lnr">172 </span><span class="Identifier">}</span>)();
<span class="lnr">173 </span>
<span class="lnr">174 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">175 </span><span class="Comment">// _.reject</span>
<span class="lnr">176 </span><span class="Comment">// _.reject(list, iterator, [context]) Alias: filter</span>
<span class="lnr">177 </span><span class="Comment">//</span>
<span class="lnr">178 </span><span class="Comment">// _.selectと逆の動作。list内の要素に対し、iteratorで定義された</span>
<span class="lnr">179 </span><span class="Comment">// 処理を実行し、true以外の要素の配列を返す。</span>
<span class="lnr">180 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">181 </span>
<span class="lnr">182 </span><span class="Comment">// listがArrayの場合</span>
<span class="lnr">183 </span>console.log(<span class="String">&quot;_reject&quot;</span>);
<span class="lnr">184 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">185 </span>  <span class="Identifier">var</span> ret = _.reject(<span class="Identifier">[</span>2,3,4,5,6<span class="Identifier">]</span>, <span class="Identifier">function</span>(num)<span class="Identifier">{</span>
<span class="lnr">186 </span>    <span class="Statement">return</span> num % <span class="Identifier">this</span>.a == 0;
<span class="lnr">187 </span>  <span class="Identifier">}</span>, <span class="Identifier">{</span>a:2<span class="Identifier">}</span>);
<span class="lnr">188 </span>  console.log(ret);
<span class="lnr">189 </span>  <span class="Comment">// #=&gt; [3,5]</span>
<span class="lnr">190 </span><span class="Identifier">}</span>)();
<span class="lnr">191 </span><span class="Comment">// listがObjectの場合</span>
<span class="lnr">192 </span>console.log(<span class="String">&quot;_reject&quot;</span>);
<span class="lnr">193 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">194 </span>  <span class="Identifier">var</span> ret = _.reject(<span class="Identifier">{</span>a:2,b:3,c:4<span class="Identifier">}</span>, <span class="Identifier">function</span>(val, key)<span class="Identifier">{</span>
<span class="lnr">195 </span>    <span class="Statement">return</span> val % <span class="Identifier">this</span>.a == 0;
<span class="lnr">196 </span>  <span class="Identifier">}</span>, <span class="Identifier">{</span>a:2<span class="Identifier">}</span>);
<span class="lnr">197 </span>  console.log(ret);
<span class="lnr">198 </span>  <span class="Comment">// #=&gt; [3]</span>
<span class="lnr">199 </span><span class="Identifier">}</span>)();
<span class="lnr">200 </span>
<span class="lnr">201 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">202 </span><span class="Comment">// _.all</span>
<span class="lnr">203 </span><span class="Comment">// _.all(list, iterator, [context]) Alias: every</span>
<span class="lnr">204 </span><span class="Comment">//</span>
<span class="lnr">205 </span><span class="Comment">// list内の要素に対し、iteratorを実行。全要素が</span>
<span class="lnr">206 </span><span class="Comment">// trueの時に trueを返す。それ以外はfalse</span>
<span class="lnr">207 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">208 </span>
<span class="lnr">209 </span><span class="Comment">// listがArrayの場合</span>
<span class="lnr">210 </span>console.log(<span class="String">&quot;_all&quot;</span>);
<span class="lnr">211 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">212 </span>  <span class="Identifier">var</span> ret = _.all(<span class="Identifier">[</span>2,3,4,5,6<span class="Identifier">]</span>, <span class="Identifier">function</span>(num)<span class="Identifier">{</span>
<span class="lnr">213 </span>    <span class="Statement">return</span> num % <span class="Identifier">this</span>.a == 0;
<span class="lnr">214 </span>  <span class="Identifier">}</span>, <span class="Identifier">{</span>a:2<span class="Identifier">}</span>);
<span class="lnr">215 </span>  console.log(ret);
<span class="lnr">216 </span>  <span class="Comment">// #=&gt; false</span>
<span class="lnr">217 </span><span class="Identifier">}</span>)();
<span class="lnr">218 </span><span class="Comment">// listがObjectの場合</span>
<span class="lnr">219 </span>console.log(<span class="String">&quot;_all&quot;</span>);
<span class="lnr">220 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">221 </span>  <span class="Identifier">var</span> ret = _.all(<span class="Identifier">{</span>a:2,b:3,c:4<span class="Identifier">}</span>, <span class="Identifier">function</span>(val, key)<span class="Identifier">{</span>
<span class="lnr">222 </span>    <span class="Statement">return</span> val &gt;= <span class="Identifier">this</span>.a;
<span class="lnr">223 </span>  <span class="Identifier">}</span>, <span class="Identifier">{</span>a:2<span class="Identifier">}</span>);
<span class="lnr">224 </span>  console.log(ret);
<span class="lnr">225 </span>  <span class="Comment">// #=&gt; true</span>
<span class="lnr">226 </span><span class="Identifier">}</span>)();
<span class="lnr">227 </span>
<span class="lnr">228 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">229 </span><span class="Comment">// _.any</span>
<span class="lnr">230 </span><span class="Comment">// _.any(list, iterator, [context]) Alias: some</span>
<span class="lnr">231 </span><span class="Comment">//</span>
<span class="lnr">232 </span><span class="Comment">// list内の要素に対し、iteratorを実行。一つでも</span>
<span class="lnr">233 </span><span class="Comment">// trueがあれば、trueを返す。trueを見つけた時点で</span>
<span class="lnr">234 </span><span class="Comment">// iterationは終了する。</span>
<span class="lnr">235 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">236 </span>
<span class="lnr">237 </span><span class="Comment">// listがArrayの場合</span>
<span class="lnr">238 </span>console.log(<span class="String">&quot;_any&quot;</span>);
<span class="lnr">239 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">240 </span>  <span class="Identifier">var</span> ret = _.any(<span class="Identifier">[</span>2,3,4,5,6<span class="Identifier">]</span>, <span class="Identifier">function</span>(num)<span class="Identifier">{</span>
<span class="lnr">241 </span>    <span class="Statement">return</span> num % <span class="Identifier">this</span>.a == 0;
<span class="lnr">242 </span>  <span class="Identifier">}</span>, <span class="Identifier">{</span>a:2<span class="Identifier">}</span>);
<span class="lnr">243 </span>  console.log(ret);
<span class="lnr">244 </span>  <span class="Comment">// #=&gt; true</span>
<span class="lnr">245 </span><span class="Identifier">}</span>)();
<span class="lnr">246 </span><span class="Comment">// listがObjectの場合</span>
<span class="lnr">247 </span>console.log(<span class="String">&quot;_any&quot;</span>);
<span class="lnr">248 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">249 </span>  <span class="Identifier">var</span> ret = _.any(<span class="Identifier">{</span>a:2,b:3,c:4<span class="Identifier">}</span>, <span class="Identifier">function</span>(val, key)<span class="Identifier">{</span>
<span class="lnr">250 </span>    <span class="Statement">return</span> val &lt; <span class="Identifier">this</span>.a;
<span class="lnr">251 </span>  <span class="Identifier">}</span>, <span class="Identifier">{</span>a:2<span class="Identifier">}</span>);
<span class="lnr">252 </span>  console.log(ret);
<span class="lnr">253 </span>  <span class="Comment">// #=&gt; false</span>
<span class="lnr">254 </span><span class="Identifier">}</span>)();
<span class="lnr">255 </span>
<span class="lnr">256 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">257 </span><span class="Comment">// _.include</span>
<span class="lnr">258 </span><span class="Comment">// _.include(list, value) Alias: contains</span>
<span class="lnr">259 </span><span class="Comment">//</span>
<span class="lnr">260 </span><span class="Comment">// list内にvalueがあれば、trueを返す(===で評価するため、</span>
<span class="lnr">261 </span><span class="Comment">// 型も含めチェック）。listがArrayの場合、内部的には</span>
<span class="lnr">262 </span><span class="Comment">// indexOfを用いている。</span>
<span class="lnr">263 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">264 </span>
<span class="lnr">265 </span><span class="Comment">// listがArrayの場合</span>
<span class="lnr">266 </span>console.log(<span class="String">&quot;_include&quot;</span>);
<span class="lnr">267 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">268 </span>  <span class="Identifier">var</span> ret = _.include(<span class="Identifier">[</span>2,3,4,5,6<span class="Identifier">]</span>, 3);
<span class="lnr">269 </span>  console.log(ret);
<span class="lnr">270 </span>  <span class="Comment">// #=&gt; true</span>
<span class="lnr">271 </span><span class="Identifier">}</span>)();
<span class="lnr">272 </span><span class="Comment">// listがObjectの場合</span>
<span class="lnr">273 </span>console.log(<span class="String">&quot;_include&quot;</span>);
<span class="lnr">274 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">275 </span>  <span class="Identifier">var</span> ret = _.include(<span class="Identifier">{</span>a:2,b:3,c:4<span class="Identifier">}</span>, 3);
<span class="lnr">276 </span>  console.log(ret);
<span class="lnr">277 </span>  <span class="Comment">// #=&gt; true</span>
<span class="lnr">278 </span><span class="Identifier">}</span>)();
<span class="lnr">279 </span>
<span class="lnr">280 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">281 </span><span class="Comment">// _.invoke</span>
<span class="lnr">282 </span><span class="Comment">// _.invoke(list, methodName, [*arguments])</span>
<span class="lnr">283 </span><span class="Comment">//</span>
<span class="lnr">284 </span><span class="Comment">// list内の要素に対し、methodNameで与えられる関数を</span>
<span class="lnr">285 </span><span class="Comment">// 実行する。argumentsは、methodNameの関数呼び出し。</span>
<span class="lnr">286 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">287 </span>
<span class="lnr">288 </span><span class="Comment">// case 1</span>
<span class="lnr">289 </span>console.log(<span class="String">&quot;_invoke&quot;</span>);
<span class="lnr">290 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">291 </span>  <span class="Identifier">var</span> ret = _.invoke(<span class="Identifier">[[</span>3,2<span class="Identifier">]</span>, <span class="Identifier">[</span>6,4<span class="Identifier">]]</span>, <span class="String">&quot;sort&quot;</span>);
<span class="lnr">292 </span>  console.log(ret);
<span class="lnr">293 </span>  <span class="Comment">// #=&gt; [[2,3], [4,6]]</span>
<span class="lnr">294 </span><span class="Identifier">}</span>)();
<span class="lnr">295 </span><span class="Comment">// case 2</span>
<span class="lnr">296 </span>console.log(<span class="String">&quot;_invoke&quot;</span>);
<span class="lnr">297 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">298 </span>  <span class="Identifier">var</span> ret = _.invoke(<span class="Identifier">[[</span>12,3<span class="Identifier">]</span>,<span class="Identifier">[</span>21, 5<span class="Identifier">]]</span>, <span class="String">&quot;sort&quot;</span>, <span class="Identifier">function</span>(a,b)<span class="Identifier">{</span><span class="Statement">return</span> a-b<span class="Identifier">}</span>);
<span class="lnr">299 </span>  console.log(ret);
<span class="lnr">300 </span>  <span class="Comment">// #=&gt; [[3,12], [5,21]]</span>
<span class="lnr">301 </span><span class="Identifier">}</span>)();
<span class="lnr">302 </span>
<span class="lnr">303 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">304 </span><span class="Comment">// _.pluck</span>
<span class="lnr">305 </span><span class="Comment">// _.pluck(list, propertyName)</span>
<span class="lnr">306 </span><span class="Comment">//</span>
<span class="lnr">307 </span><span class="Comment">// listに対し、propertyNameで指定された値の配列を</span>
<span class="lnr">308 </span><span class="Comment">// 返す</span>
<span class="lnr">309 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">310 </span>
<span class="lnr">311 </span><span class="Comment">//</span>
<span class="lnr">312 </span>console.log(<span class="String">&quot;_pluck&quot;</span>);
<span class="lnr">313 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">314 </span>  <span class="Identifier">var</span> ret = _.pluck(<span class="Identifier">[{</span>a:1, b:2<span class="Identifier">}</span>, <span class="Identifier">{</span>a:5, b:7<span class="Identifier">}]</span>, <span class="String">&quot;a&quot;</span>);
<span class="lnr">315 </span>  console.log(ret);
<span class="lnr">316 </span>  <span class="Comment">// #=&gt; [1,5]</span>
<span class="lnr">317 </span><span class="Identifier">}</span>)();
<span class="lnr">318 </span>
<span class="lnr">319 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">320 </span><span class="Comment">// _.max</span>
<span class="lnr">321 </span><span class="Comment">// _.max(list, [iterator], [context])</span>
<span class="lnr">322 </span><span class="Comment">//</span>
<span class="lnr">323 </span><span class="Comment">// list中の最大値を返す。iteratorにより、判定基準を</span>
<span class="lnr">324 </span><span class="Comment">// 指定可能</span>
<span class="lnr">325 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">326 </span>
<span class="lnr">327 </span><span class="Comment">// case wo/ iterator</span>
<span class="lnr">328 </span>console.log(<span class="String">&quot;_max&quot;</span>);
<span class="lnr">329 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">330 </span>  <span class="Identifier">var</span> ret = _.max(<span class="Identifier">[</span>2,6,5<span class="Identifier">]</span>);
<span class="lnr">331 </span>  console.log(ret);
<span class="lnr">332 </span>  <span class="Comment">// #=&gt; 6</span>
<span class="lnr">333 </span><span class="Identifier">}</span>)();
<span class="lnr">334 </span><span class="Comment">// case 2/ iterator</span>
<span class="lnr">335 </span>console.log(<span class="String">&quot;_max&quot;</span>);
<span class="lnr">336 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">337 </span>  <span class="Identifier">var</span> ret = _.max(<span class="Identifier">[{</span>name:<span class="String">&quot;hoge&quot;</span>, price:1000<span class="Identifier">}</span>,<span class="Identifier">{</span>name:<span class="String">&quot;fuga&quot;</span>, price:500<span class="Identifier">}]</span>, <span class="Identifier">function</span>(obj)<span class="Identifier">{</span><span class="Statement">return</span> obj.price;<span class="Identifier">}</span>);
<span class="lnr">338 </span>  console.log(ret);
<span class="lnr">339 </span>  <span class="Comment">// #=&gt; {name:&quot;hoge&quot;, price:1000}</span>
<span class="lnr">340 </span><span class="Identifier">}</span>)();
<span class="lnr">341 </span>
<span class="lnr">342 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">343 </span><span class="Comment">// _.min</span>
<span class="lnr">344 </span><span class="Comment">// _.min(list, [iterator], [context])</span>
<span class="lnr">345 </span><span class="Comment">//</span>
<span class="lnr">346 </span><span class="Comment">// list中の最大値を返す。iteratorにより、判定基準を</span>
<span class="lnr">347 </span><span class="Comment">// 指定可能</span>
<span class="lnr">348 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">349 </span>
<span class="lnr">350 </span><span class="Comment">// case wo/ iterator</span>
<span class="lnr">351 </span>console.log(<span class="String">&quot;_min&quot;</span>);
<span class="lnr">352 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">353 </span>  <span class="Identifier">var</span> ret = _.min(<span class="Identifier">[</span>2,6,5<span class="Identifier">]</span>);
<span class="lnr">354 </span>  console.log(ret);
<span class="lnr">355 </span>  <span class="Comment">// #=&gt; 2</span>
<span class="lnr">356 </span><span class="Identifier">}</span>)();
<span class="lnr">357 </span><span class="Comment">// case 2/ iterator</span>
<span class="lnr">358 </span>console.log(<span class="String">&quot;_min&quot;</span>);
<span class="lnr">359 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">360 </span>  <span class="Identifier">var</span> ret = _.min(<span class="Identifier">[{</span>name:<span class="String">&quot;hoge&quot;</span>, price:1000<span class="Identifier">}</span>,<span class="Identifier">{</span>name:<span class="String">&quot;fuga&quot;</span>, price:500<span class="Identifier">}]</span>, <span class="Identifier">function</span>(obj)<span class="Identifier">{</span><span class="Statement">return</span> obj.price;<span class="Identifier">}</span>);
<span class="lnr">361 </span>  console.log(ret);
<span class="lnr">362 </span>  <span class="Comment">// #=&gt; {name:&quot;fuga&quot;, price:500}</span>
<span class="lnr">363 </span><span class="Identifier">}</span>)();
<span class="lnr">364 </span>
<span class="lnr">365 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">366 </span><span class="Comment">// _.sortBy</span>
<span class="lnr">367 </span><span class="Comment">// _.sortBy(list, iterator, [context])</span>
<span class="lnr">368 </span><span class="Comment">//</span>
<span class="lnr">369 </span><span class="Comment">// listに対し、iteratorの演算結果でソートする</span>
<span class="lnr">370 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">371 </span>
<span class="lnr">372 </span>console.log(<span class="String">&quot;_sortBy&quot;</span>);
<span class="lnr">373 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">374 </span>  <span class="Identifier">var</span> ret = _.sortBy(<span class="Identifier">[{</span>name:<span class="String">&quot;hoge&quot;</span>, price:1000<span class="Identifier">}</span>,<span class="Identifier">{</span>name:<span class="String">&quot;fuga&quot;</span>,price:500<span class="Identifier">}]</span>, <span class="Identifier">function</span>(obj)<span class="Identifier">{</span><span class="Statement">return</span> obj.price<span class="Identifier">}</span>);
<span class="lnr">375 </span>  console.log(ret);
<span class="lnr">376 </span>  <span class="Comment">// #=&gt; [{name:&quot;fuga&quot;,price:500},{name:&quot;hoge&quot;,price:1000}]</span>
<span class="lnr">377 </span><span class="Identifier">}</span>)();
<span class="lnr">378 </span>
<span class="lnr">379 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">380 </span><span class="Comment">// _.sortedIndex</span>
<span class="lnr">381 </span><span class="Comment">// _.sortedIndex(list, value, [iterator])</span>
<span class="lnr">382 </span><span class="Comment">//</span>
<span class="lnr">383 </span><span class="Comment">// listの中で、valueが何番目に入るかを返す。ソートされた</span>
<span class="lnr">384 </span><span class="Comment">// listのオーダーを保持するのに有効。iteratorが渡された</span>
<span class="lnr">385 </span><span class="Comment">// 場合は、listのソート計算に用いられる。</span>
<span class="lnr">386 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">387 </span>
<span class="lnr">388 </span>console.log(<span class="String">&quot;_sortedIndex&quot;</span>);
<span class="lnr">389 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">390 </span>  <span class="Identifier">var</span> ret = _.sortedIndex(<span class="Identifier">[</span>10,20,30,40<span class="Identifier">]</span>, 35);
<span class="lnr">391 </span>  console.log(ret);
<span class="lnr">392 </span>  <span class="Comment">// #=&gt; 3</span>
<span class="lnr">393 </span><span class="Identifier">}</span>)();
<span class="lnr">394 </span>
<span class="lnr">395 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">396 </span><span class="Comment">// _.toArray</span>
<span class="lnr">397 </span><span class="Comment">// _.toArray(list)</span>
<span class="lnr">398 </span><span class="Comment">//</span>
<span class="lnr">399 </span><span class="Comment">// listを配列に変換する。argumentsの変換に便利。</span>
<span class="lnr">400 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">401 </span>
<span class="lnr">402 </span>console.log(<span class="String">&quot;_toArray&quot;</span>);
<span class="lnr">403 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">404 </span>  <span class="Identifier">var</span> ret = (<span class="Identifier">function</span>()<span class="Identifier">{</span> <span class="Statement">return</span> _.toArray(<span class="Identifier">arguments</span>); <span class="Identifier">}</span>)(1,2,3);
<span class="lnr">405 </span>  console.log(ret);
<span class="lnr">406 </span>  <span class="Comment">// #=&gt; [1,2,3]</span>
<span class="lnr">407 </span><span class="Identifier">}</span>)();
<span class="lnr">408 </span>
<span class="lnr">409 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">410 </span><span class="Comment">// _.size</span>
<span class="lnr">411 </span><span class="Comment">// _.size(list)</span>
<span class="lnr">412 </span><span class="Comment">//</span>
<span class="lnr">413 </span><span class="Comment">// listの長さを返す。</span>
<span class="lnr">414 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">415 </span>
<span class="lnr">416 </span>console.log(<span class="String">&quot;_size&quot;</span>);
<span class="lnr">417 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">418 </span>  <span class="Identifier">var</span> ret = _.size(<span class="Identifier">{</span>a:1, b:2, c:1<span class="Identifier">}</span>);
<span class="lnr">419 </span>  console.log(ret);
<span class="lnr">420 </span>  <span class="Comment">// #=&gt; 3</span>
<span class="lnr">421 </span><span class="Identifier">}</span>)();
<span class="lnr">422 </span>
<span class="lnr">423 </span><span class="Comment">/**</span>
<span class="lnr">424 </span><span class="Comment"> * Arrays</span>
<span class="lnr">425 </span><span class="Comment"> *</span>
<span class="lnr">426 </span><span class="Comment"> */</span>
<span class="lnr">427 </span>
<span class="lnr">428 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">429 </span><span class="Comment">// _.first</span>
<span class="lnr">430 </span><span class="Comment">// _.first(array, [n]) Alias: head</span>
<span class="lnr">431 </span><span class="Comment">//</span>
<span class="lnr">432 </span><span class="Comment">// arrayの最初の要素を返す。nを指定した場合、最初から</span>
<span class="lnr">433 </span><span class="Comment">// n個までの配列を返す。</span>
<span class="lnr">434 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">435 </span>
<span class="lnr">436 </span>console.log(<span class="String">&quot;_.first&quot;</span>);
<span class="lnr">437 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">438 </span>  <span class="Identifier">var</span> ret = _.first(<span class="Identifier">[</span>1,2,3,4,5<span class="Identifier">]</span>);
<span class="lnr">439 </span>  console.log(ret);
<span class="lnr">440 </span>  <span class="Comment">// #=&gt; 1</span>
<span class="lnr">441 </span>  ret = _.first(<span class="Identifier">[</span>1,2,3,4,5<span class="Identifier">]</span>, 2);
<span class="lnr">442 </span>  console.log(ret);
<span class="lnr">443 </span>  <span class="Comment">// #=&gt; [1,2]</span>
<span class="lnr">444 </span><span class="Identifier">}</span>)();
<span class="lnr">445 </span>
<span class="lnr">446 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">447 </span><span class="Comment">// _.rest</span>
<span class="lnr">448 </span><span class="Comment">// _.rest(array, [n]) Alias: tail</span>
<span class="lnr">449 </span><span class="Comment">//</span>
<span class="lnr">450 </span><span class="Comment">// arrayの残りの要素を返す。nを指定すると、n番目以降の</span>
<span class="lnr">451 </span><span class="Comment">// 配列が返される。</span>
<span class="lnr">452 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">453 </span>
<span class="lnr">454 </span>console.log(<span class="String">&quot;_.rest&quot;</span>);
<span class="lnr">455 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">456 </span>  <span class="Identifier">var</span> ret = _.rest(<span class="Identifier">[</span>1,2,3,4,5<span class="Identifier">]</span>);
<span class="lnr">457 </span>  console.log(ret);
<span class="lnr">458 </span>  <span class="Comment">// #=&gt; 1</span>
<span class="lnr">459 </span>  ret = _.rest(<span class="Identifier">[</span>1,2,3,4,5<span class="Identifier">]</span>, 2);
<span class="lnr">460 </span>  console.log(ret);
<span class="lnr">461 </span>  <span class="Comment">// #=&gt; [1,2]</span>
<span class="lnr">462 </span><span class="Identifier">}</span>)();
<span class="lnr">463 </span>
<span class="lnr">464 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">465 </span><span class="Comment">// _.last</span>
<span class="lnr">466 </span><span class="Comment">// _.last(array)</span>
<span class="lnr">467 </span><span class="Comment">//</span>
<span class="lnr">468 </span><span class="Comment">// arrayの最後の要素を返す。</span>
<span class="lnr">469 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">470 </span>
<span class="lnr">471 </span>console.log(<span class="String">&quot;_.last&quot;</span>);
<span class="lnr">472 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">473 </span>  <span class="Identifier">var</span> ret = _.last(<span class="Identifier">[</span>1,2,3,4,5<span class="Identifier">]</span>);
<span class="lnr">474 </span>  console.log(ret);
<span class="lnr">475 </span>  <span class="Comment">// #=&gt; 5</span>
<span class="lnr">476 </span><span class="Identifier">}</span>)();
<span class="lnr">477 </span>
<span class="lnr">478 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">479 </span><span class="Comment">// _.compact</span>
<span class="lnr">480 </span><span class="Comment">// _.compact(array)</span>
<span class="lnr">481 </span><span class="Comment">//</span>
<span class="lnr">482 </span><span class="Comment">// arrayから、偽になる値を削除する。false, null, 0, &quot;&quot;,</span>
<span class="lnr">483 </span><span class="Comment">// undefinedとNaNが偽である。</span>
<span class="lnr">484 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">485 </span>
<span class="lnr">486 </span>console.log(<span class="String">&quot;_.compact&quot;</span>);
<span class="lnr">487 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">488 </span>  <span class="Identifier">var</span> ret = _.compact(<span class="Identifier">[</span>1,<span class="Boolean">false</span>,2,0,4,<span class="Statement">undefined</span><span class="Identifier">]</span>);
<span class="lnr">489 </span>  console.log(ret);
<span class="lnr">490 </span>  <span class="Comment">// #=&gt; [1,2,4]</span>
<span class="lnr">491 </span><span class="Identifier">}</span>)();
<span class="lnr">492 </span>
<span class="lnr">493 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">494 </span><span class="Comment">// _.flatten</span>
<span class="lnr">495 </span><span class="Comment">// _.flatten(array)</span>
<span class="lnr">496 </span><span class="Comment">//</span>
<span class="lnr">497 </span><span class="Comment">// ネストされた array をフラットにする。</span>
<span class="lnr">498 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">499 </span>
<span class="lnr">500 </span>console.log(<span class="String">&quot;_.flatten&quot;</span>);
<span class="lnr">501 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">502 </span>  <span class="Identifier">var</span> ret = _.flatten(<span class="Identifier">[</span>1,<span class="Identifier">[</span>2<span class="Identifier">]</span>,<span class="Identifier">[</span>3, <span class="Identifier">[[</span>4<span class="Identifier">]]]]</span>);
<span class="lnr">503 </span>  console.log(ret);
<span class="lnr">504 </span>  <span class="Comment">// #=&gt; [1,2,3,4]</span>
<span class="lnr">505 </span><span class="Identifier">}</span>)();
<span class="lnr">506 </span>
<span class="lnr">507 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">508 </span><span class="Comment">// _.without</span>
<span class="lnr">509 </span><span class="Comment">// _.without(array, [*value])</span>
<span class="lnr">510 </span><span class="Comment">//</span>
<span class="lnr">511 </span><span class="Comment">// arrayからvalueで指定した値を削除する。値のチェックに</span>
<span class="lnr">512 </span><span class="Comment">// === を用いている。</span>
<span class="lnr">513 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">514 </span>
<span class="lnr">515 </span>console.log(<span class="String">&quot;_.without&quot;</span>);
<span class="lnr">516 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">517 </span>  <span class="Identifier">var</span> ret = _.without(<span class="Identifier">[</span>1,2,0,3,2,<span class="String">&quot;1&quot;</span><span class="Identifier">]</span>, 0, 1);
<span class="lnr">518 </span>  console.log(ret);
<span class="lnr">519 </span>  <span class="Comment">// #=&gt; [2,3,2,&quot;1&quot;]</span>
<span class="lnr">520 </span><span class="Identifier">}</span>)();
<span class="lnr">521 </span>
<span class="lnr">522 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">523 </span><span class="Comment">// _.uniq</span>
<span class="lnr">524 </span><span class="Comment">// _.uniq(array, [isSorted])</span>
<span class="lnr">525 </span><span class="Comment">//</span>
<span class="lnr">526 </span><span class="Comment">// arrayから重複値を削除し、ユニークな値による配列を返す。</span>
<span class="lnr">527 </span><span class="Comment">// arrayがsortされているばあい、isSortedをtrueに設定すると、</span>
<span class="lnr">528 </span><span class="Comment">// 高速なアルゴリズムが適用される。</span>
<span class="lnr">529 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">530 </span>
<span class="lnr">531 </span>console.log(<span class="String">&quot;_.uniq&quot;</span>);
<span class="lnr">532 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">533 </span>  <span class="Identifier">var</span> ret = _.uniq(<span class="Identifier">[</span>2,4,1,4,0,2<span class="Identifier">]</span>);
<span class="lnr">534 </span>  console.log(ret);
<span class="lnr">535 </span>  <span class="Comment">// #=&gt; [2,4,1,0]</span>
<span class="lnr">536 </span><span class="Identifier">}</span>)();
<span class="lnr">537 </span>
<span class="lnr">538 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">539 </span><span class="Comment">// _.intersect</span>
<span class="lnr">540 </span><span class="Comment">// _.intersect(*arrays)</span>
<span class="lnr">541 </span><span class="Comment">//</span>
<span class="lnr">542 </span><span class="Comment">// arraysの積集合（共通の値）による配列を返す。</span>
<span class="lnr">543 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">544 </span>
<span class="lnr">545 </span>console.log(<span class="String">&quot;_.intersect&quot;</span>);
<span class="lnr">546 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">547 </span>  <span class="Identifier">var</span> ret = _.intersect(<span class="Identifier">[</span>1,2,3<span class="Identifier">]</span>, <span class="Identifier">[</span>3,1,4<span class="Identifier">]</span>);
<span class="lnr">548 </span>  console.log(ret);
<span class="lnr">549 </span>  <span class="Comment">// #=&gt; [1,3]</span>
<span class="lnr">550 </span><span class="Identifier">}</span>)();
<span class="lnr">551 </span>
<span class="lnr">552 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">553 </span><span class="Comment">// _.zip</span>
<span class="lnr">554 </span><span class="Comment">// _.zip(*arrays)</span>
<span class="lnr">555 </span><span class="Comment">//</span>
<span class="lnr">556 </span><span class="Comment">// arraysのindex値が一致する値をマージする。</span>
<span class="lnr">557 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">558 </span>
<span class="lnr">559 </span>console.log(<span class="String">&quot;_.zip&quot;</span>);
<span class="lnr">560 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">561 </span>  <span class="Identifier">var</span> ret = _.zip(<span class="Identifier">[</span><span class="String">'a'</span>, <span class="String">'b'</span>, <span class="String">'c'</span><span class="Identifier">]</span>, <span class="Identifier">[</span>30,40,50<span class="Identifier">]</span>,<span class="Identifier">[</span><span class="Boolean">true</span>, <span class="Boolean">false</span>, <span class="Boolean">true</span><span class="Identifier">]</span>);
<span class="lnr">562 </span>  console.log(ret);
<span class="lnr">563 </span>  <span class="Comment">// #=&gt; [['a',30,true],['b',40,false],['c',50,true]]</span>
<span class="lnr">564 </span><span class="Identifier">}</span>)();
<span class="lnr">565 </span>
<span class="lnr">566 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">567 </span><span class="Comment">// _.indexOf</span>
<span class="lnr">568 </span><span class="Comment">// _.indexOf(array, value, [isSorted])</span>
<span class="lnr">569 </span><span class="Comment">//</span>
<span class="lnr">570 </span><span class="Comment">// arrayの中のvalueの位置を返す。valueが存在しない場合は、</span>
<span class="lnr">571 </span><span class="Comment">// -1 を返す。arrayがソートされている場合は、isSortedを</span>
<span class="lnr">572 </span><span class="Comment">// trueにすると、高速な binary search(二分探索）を行う。</span>
<span class="lnr">573 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">574 </span>
<span class="lnr">575 </span>console.log(<span class="String">&quot;_.indexOf&quot;</span>);
<span class="lnr">576 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">577 </span>  <span class="Identifier">var</span> ret = _.indexOf(<span class="Identifier">[</span>1,4,2<span class="Identifier">]</span>, 4)
<span class="lnr">578 </span>  console.log(ret);
<span class="lnr">579 </span>  <span class="Comment">// #=&gt; 1</span>
<span class="lnr">580 </span><span class="Identifier">}</span>)();
<span class="lnr">581 </span>
<span class="lnr">582 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">583 </span><span class="Comment">// _.lastIndexOf</span>
<span class="lnr">584 </span><span class="Comment">// _.lastIndexOf(array, value)</span>
<span class="lnr">585 </span><span class="Comment">//</span>
<span class="lnr">586 </span><span class="Comment">// array中で、valueが最後に出現するインデックス値を返す。</span>
<span class="lnr">587 </span><span class="Comment">// 見つからない場合は -1を返す。</span>
<span class="lnr">588 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">589 </span>
<span class="lnr">590 </span>console.log(<span class="String">&quot;_.lastIndexOf&quot;</span>);
<span class="lnr">591 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">592 </span>  <span class="Identifier">var</span> ret = _.lastIndexOf(<span class="Identifier">[</span>1,4,2,4<span class="Identifier">]</span>, 4)
<span class="lnr">593 </span>  console.log(ret);
<span class="lnr">594 </span>  <span class="Comment">// #=&gt; 3</span>
<span class="lnr">595 </span><span class="Identifier">}</span>)();
<span class="lnr">596 </span>
<span class="lnr">597 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">598 </span><span class="Comment">// _.range</span>
<span class="lnr">599 </span><span class="Comment">// _.range([start], stop, [step])</span>
<span class="lnr">600 </span><span class="Comment">//</span>
<span class="lnr">601 </span><span class="Comment">// 整数値による連続した配列を返す。eachやmapに用いるのに</span>
<span class="lnr">602 </span><span class="Comment">// 便利である。start が省略された場合のdefaultは0, stepが</span>
<span class="lnr">603 </span><span class="Comment">// 省略された場合のdefaultは1である。startからstopまで、</span>
<span class="lnr">604 </span><span class="Comment">// step毎に増加もしくは減少する整数値を返す。</span>
<span class="lnr">605 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">606 </span>
<span class="lnr">607 </span>console.log(<span class="String">&quot;_.range&quot;</span>);
<span class="lnr">608 </span>(<span class="Identifier">function</span>()<span class="Identifier">{</span>
<span class="lnr">609 </span>  console.log(_.range(10));
<span class="lnr">610 </span>  <span class="Comment">// #=&gt; [0,1,2,3,4,5,6,7,8,9]</span>
<span class="lnr">611 </span>  console.log(_.range(1,11));
<span class="lnr">612 </span>  <span class="Comment">// #=&gt; [1,2,3,4,5,6,7,8,9,10]</span>
<span class="lnr">613 </span>  console.log(_.range(0,30,5));
<span class="lnr">614 </span>  <span class="Comment">// #=&gt; [0,5,10,15,20,25]</span>
<span class="lnr">615 </span>  console.log(_.range(0,-10,-1));
<span class="lnr">616 </span>  <span class="Comment">// #=&gt; [0,-1,-2,-3,-4,-5,-6,-7,-8,-9]</span>
<span class="lnr">617 </span><span class="Identifier">}</span>)();
<span class="lnr">618 </span>
<span class="lnr">619 </span>
<span class="lnr">620 </span>
<span class="lnr">621 </span>
<span class="lnr">622 </span><span class="Comment">/**</span>
<span class="lnr">623 </span><span class="Comment"> * Functions</span>
<span class="lnr">624 </span><span class="Comment"> *</span>
<span class="lnr">625 </span><span class="Comment"> */</span>
<span class="lnr">626 </span>
<span class="lnr">627 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">628 </span><span class="Comment">// _.bind</span>
<span class="lnr">629 </span><span class="Comment">// _.bind(function, object, [*arguments])</span>
<span class="lnr">630 </span><span class="Comment">//</span>
<span class="lnr">631 </span><span class="Comment">// functionをobjectにbindする。このとき、thisはobjectを指す。</span>
<span class="lnr">632 </span><span class="Comment">// argumentsをbindすること(カリー化)も可能。</span>
<span class="lnr">633 </span><span class="Comment">/////////////////////////////////////////////////</span>
<span class="lnr">634 </span>
<span class="lnr">635 </span><span class="Identifier">var</span> func = <span class="Identifier">function</span>(item)<span class="Identifier">{</span><span class="Statement">return</span> item + <span class="String">&quot; : &quot;</span>+<span class="Identifier">this</span>.price+<span class="String">&quot; Yen&quot;</span><span class="Identifier">}</span>
<span class="lnr">636 </span>func = _.bind(func, <span class="Identifier">{</span>price: 100<span class="Identifier">}</span>, <span class="String">'pen'</span>);
<span class="lnr">637 </span>console.log(func());
<span class="lnr">638 </span><span class="Comment">// #=&gt; 'pen : 100 Yen'</span>
<span class="lnr">639 </span>
<span class="lnr">640 </span>
</pre>
</body>
</html>
